NSMobile.models.NewsItem = Ext.regModel('NewsItem', {
    fields: [
        { name: 'id', type: 'int' },
        { name: 'date', type: 'string' },
        { name: 'title', type: 'string' },
        { name: 'text', type: 'string' },
        { name: 'type', type: 'int' },
		{ name: 'summary', type: 'string' },
        { name: 'thumb', type: 'string' },
        { name: 'read', type: 'boolean', defaultValue: false },
        { name: 'hidden', type: 'boolean', defaultValue: false },
        'language',
        'country',
        'site'
    ],
    markRead: function () {
        if (!this.data.read) {
            var storageData = getStorage('news');

            var newsStatus = storageData.news[this.data.id] || {};
            newsStatus.read = true;

            if (this.data.type == 2)
                unreadAlerts--;
            else
                unreadNews--;

            var newsTab = Ext.getCmp('news').tab;
            if (unreadAlerts + unreadNews > 0)
                newsTab.setBadge(unreadAlerts + unreadNews);
            else
                newsTab.setBadge('');

            var newClass = 'newsTitle newsTitle2-viewed';
            this.data.read = true;

            storageData.news[this.data.id] = newsStatus;
            setStorage('news', storageData);
            Ext.getCmp('newslist').refresh();
        }
    },
    hide: function () {
        var storageData = getStorage('news');
        var newsStatus = storageData.news[this.data.id] || {};
        newsStatus.hidden = this.data.hidden = true;
        storageData.news[this.data.id] = newsStatus;
        setStorage('news', storageData);
        Ext.getCmp('newslist').refresh();
    }
});

NSMobile.stores.news = new Ext.data.Store({
    model: 'NewsItem',
    proxy: {
        type: 'ajax',
        url: nsServiceURI + '/GetNews',
        listeners: {
            exception: {
                fn: function (proxy, response, operation) {
                    showErrorOnce();
                }
            }
        },
        extraParams: {
            country: getStorage('auth').country,
            language: getStorage('auth').language
        },
        timeout: ajaxTimeout
    },
    listeners: {
        beforeload: {
            fn: function () {
                setSyncIcon('home', true);
            }
        },
        load: {
            fn: function (store, records, successful) {
                var storageData = getStorage('news');
                storageData.news = storageData.news || {};
                var news = {};
                for (var i = 0; i < NSMobile.stores.news.data.length; i++) {
                    var newsItem = NSMobile.stores.news.getAt(i);
                    if (storageData.news[newsItem.data.id]) {
                        newsItem.data.read = storageData.news[newsItem.data.id].read;
                        newsItem.data.hidden = storageData.news[newsItem.data.id].hidden;
                    }
                    news[newsItem.data.id] = newsItem.data;
                }
                Ext.apply(storageData.news, news);
                storageData.lastUpdate = new Date();
                setStorage('news', storageData);
                newsLoaded = true;
                NSMobile.stores.local.news.load();

                setSyncIcon('home', false);
            }
        }
    }
});

NSMobile.stores.local.news = new Ext.data.Store({
    model: 'NewsItem',
    proxy: {
        type: 'memory',
        reader: {
            type: 'json'
        },
        read: function (operation, callback, scope) {
            var storageData = getStorage('news');
            var news = [];
            unreadAlerts = 0;
            unreadNews = 0;

            for (var key in storageData.news) {
                var newsItem = storageData.news[key];
                news.push(new this.model(newsItem));
                if (!newsItem.hidden && !newsItem.read) {
                    if (newsItem.type == 1)
                        unreadNews++;
                    else
                        unreadAlerts++;
                }
            }

            if (!newsLoaded) {
                if (news.length > 0)
                    NSMobile.stores.news.getProxy().extraParams.lastUpdate = storageData.lastUpdate;

                NSMobile.stores.news.load();
                NSMobile.stores.news.getProxy().extraParams.lastUpdate = undefined;
            }

            var newsCmp = Ext.getCmp('news');
            if (newsCmp)
                newsCmp.tab.setBadge(unreadAlerts + unreadNews);

            operation.resultSet = new Ext.data.ResultSet({
                records: news,
                total: news.length,
                loaded: true
            });

            operation.setSuccessful();
            operation.setCompleted();

            if (typeof callback == 'function') {
                callback.call(scope || this, operation);
            }
        }
    },
    sorters: {
        property: 'id',
        direction: 'desc'
    },
    listeners: {
        load: {
            fn: function (store, records, successful) {
                store.snapshot = store.data; //workaround sencha bug causing filterBy to clear the store
                NSMobile.stores.news.filter('hidden', false);
            }
        }
    },
    getGroupString: function (record) {
        var groupString = '';
        if (record.data.type == 1)
            groupString += 'News: (<span id=\'newsHeaderCount\'>' + unreadNews + '</span>)';
        else
            groupString += 'Alerts: (<span id=\'alertsHeaderCount\'>' + unreadAlerts + '</span>)';

        return groupString;
    },
    filters: [
        {
            property: 'hidden',
            value: false
        }
    ]
});

var unreadAlerts = 0, unreadNews = 0, newsLoaded = false;